<?php
/**
 * 轮播图模型
 */

namespace app\api\model;

use custom\Err;
use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Error;
use think\exception\DbException;
use think\Model;
use app\api\controller\Config;
use app\common\lib\RespCode;
use think\Exception;

class Dynamic extends Model {


    /**
     * 动态列表数据
     * @param $params
     * @param $pageSize
     * @return mixed
     */
   public function getDynamicList($params,$pageSize){
       $data=db('agent_trends')
           ->alias('a')
           ->field('a.id trends_id,a.content,a.pics,a.see_num,a.give_num,a.init_see_num,a.init_give_num,a.itime,c.name,c.cover')
           ->join('company_agent c','c.id=a.agent_id','left')
           ->where("a.agent_id={$params['agent_id']}")
           ->page($params['page'],$pageSize)
           ->order('a.itime desc')
           ->select();
       foreach ($data as $key =>$value){
           $data[$key]['itime']=date('Y-m-d H:i:s',$value['itime']);
           $data[$key]['pics']=$value['pics']?explode(',',$value['pics']):[];
           $data[$key]['see_num']=(int)($value['see_num']+$value['init_see_num']);
           $data[$key]['give_num']=(int)($value['give_num']+$value['init_give_num']);
           $data[$key]['content']=$value['content']?base64_decode($value['content']):'';
       }
       $count=db('agent_trends')
           ->alias('a')
           ->join('company_agent c','c.id=a.agent_id','left')
           ->where("a.agent_id={$params['agent_id']}")
           ->count();
       if ($pageSize > 0) {
           $pageNum = ceil($count / $pageSize);
       } else {
           $pageNum = 1;
       }
       $result['count'] = $count;
       $result['page'] = (int)$params['page'];
       $result['pageNum'] = $pageNum;
       $result['data'] = $data;
       return $result;

   }

    /**
     * 动态保存
     * @param $params
     * @return int|string
     */
   public function saveDynamic($params){
       $count=db('agent_trends')->where("agent_id={$params['agent_id']}")->count();
       if ($count>=20){
           Err::add("动态最多添加20条",RespCode::PARAMS_ERR);
           return false;
       }
       $add['agent_id']=$params['agent_id'];//经纪人id
       $add['content']=isset($params['content'])?base64_encode($params['content']):'';//文字描述
       $add['pics']=isset($params['pics'])?$params['pics']:'';//图片最多9张（逗号隔开）
       $add['init_see_num']=0;//初始查看数（虚拟）
       $add['init_give_num']=0;//初始点赞（虚拟）
       $add['see_num']=0;//真实查看数
       $add['give_num']=0;//真实点赞数
       $add['itime']=time();//添加时间
       $re=db('agent_trends')->insert($add);
       if ($re){
           return true;
       }else{
           Err::add("保存失败",RespCode::QUERY_ERR);
           return false;
       }

   }

    /**
     * 删除动态
     * @param $params
     * @return int
     */
   public function delDynamic($params){
        $del=db('agent_trends')->where("id={$params['trends_id']}")->delete();
        return $del;
   }
}
